/***
 * @Author: LVGRAPE
 * @Date: 2023-09-20 18:42:54
 * @LastEditTime: 2023-09-20 18:47:32
 * @LastEditors: LVGRAPE
 * @Description:
 * @FilePath: \ZINO_FC_V4\ZINO\inc\rc.h
 * @要啥没啥，爱咋咋的
 */

#ifndef __RC_H_
#define __RC_H_
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus

#include "si24r1.h"
#include <stdbool.h>

#define RTX_BUFFER_SIZE 20
    enum rc_mode {
        RC_MODE_NULL = 0,
        RC_MODE_PAIRING,
        RC_MODE_NORMAL,
    };
    typedef struct zino_common_rc_buttom
    {
        /**两个字节16位，用到了14个按键，1按下，0没有按下 */
        bool LL : 1;  //左边按键的左
        bool LR : 1;  //左边按键的右
        bool LD : 1;  //左边按键的下
        bool LU : 1;  //左边按键的上
        bool RL : 1;  //右边按键的左
        bool RR : 1;  //右边按键的右
        bool RD : 1;  //右边按键的下
        bool RU : 1;  //右边按键的上
        bool S12 : 1; //按键S12
        bool S13 : 1; //按键S13
        bool S14 : 1; //按键S14
        bool S15 : 1; //按键S15
        bool S16 : 1; //按键S16
        bool S17 : 1; //按键S17
        bool S18 : 1; //按键S18
        bool S19 : 1; //按键S19
    } ZINO_COMMON_RC_BUTTOM_S;
    typedef struct zino_common_rc_stitcks
    {
        uint16_t LLR; //左边摇杆的左右，每个通道±2048
        uint16_t LUD; //左边摇杆的上下，每个通道±2048
        uint16_t RLR; //右边摇杆的左右，每个通道±2048
        uint16_t RUD; //右边摇杆的上下，每个通道±2048
    } ZINO_COMMON_RC_STICK_S;
    typedef union ZINO_COMMON_RC_CHANNLE
    {
        struct channel
        {
            uint8_t head;
            ZINO_COMMON_RC_BUTTOM_S buttom;
            ZINO_COMMON_RC_STICK_S stick;
            uint32_t timestamp;
            uint8_t mode;
            uint8_t crc;
        } __attribute__((packed)) values;
        uint8_t raw[17];
    } ZINO_COMMON_RC_CHANNLE_s;
    typedef union wl_msg
    {
        struct {
            uint8_t head;
            uint8_t index;
            uint16_t channel[8];
            uint8_t tail;
            uint8_t crc;
        }v;
        uint8_t raw[RTX_BUFFER_SIZE];
    }wl_msg_t;
    typedef enum RC_ALL_STATUS
    {
        /*发送中*/
        RC_TRANSMITTING = 0,
        //发送完
        RC_TRANSMITTED,
        //接收中
        RC_RECEIVING,
        //接收完
        RC_RECEIVED,
        /*对频中*/
        RC_PAIRING,
        //对频完
        RC_PAIRED,
        //对频失败
        RC_PAIRFAIL,
        //超时
        RC_TIMEOUT,
        RC_RX_TIMEOUT,


    } RC_STATUS_E;
    enum rc_event{
        RC_EVENT_LOST = 0X01,
        RC_EVENT_GET_MSG = 0X02,
        RC_EVENT_PAIRING_SUCCESS = 0X04,
        RC_EVENT_PAIRING_FAIL = 0X08,
        RC_EVENT_ALL = 0XFFFFFFFF,
    };
    struct rc_handle {
        struct si24r1 si24r1;
        enum rc_mode mode;
        RC_STATUS_E status;
        rt_uint8_t rf_channel;
        rt_uint8_t pair_channel;
        rt_uint8_t pair_handshake_byte;
        rt_uint8_t pair_handshake_byte_v1;
        rt_uint8_t pair_fc_handshake_byte;
        rt_uint8_t pair_rc_handshake_byte;
        rt_uint8_t pair_addr[5];
        rt_uint16_t pair_sync_time;
        rt_uint16_t pair_wait_time;
        rt_uint8_t rssi;
        rt_uint8_t hopping[5];
        rt_uint8_t address[5];
        uint16_t rx_lost;
        wl_msg_t rx_msg;
        wl_msg_t tx_msg;
        rt_uint16_t rxCnt;
        rt_uint16_t rx_rssi;
        rt_timer_t second_timer;
        rt_event_t event;
    };
    struct rc_config {
        rt_uint8_t address[5];
    };

#ifdef __cplusplus
}
#endif // __cplusplus
#endif
